package com.hw.ui.relation_info;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.hw.R;
import com.hw.base.activity.BaseActivity;
import com.hw.base.util.Util;
import com.hw.bean.AbScanfBean;
import com.hw.daobean.ProductInfo;
import com.hw.tool.DateUtils;
import com.hw.ui.relation_scanf.RelationScanfActivity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RelationInfoActivity extends BaseActivity<RelationInfoModel, RelationInfoView, RelationInfoPresent> implements RelationInfoView {
    private String tag = "RelationInfoActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.etp)
    EditText etp;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.etbatch)
    EditText etbatch;
    @Bind(R.id.btbatch)
    Button btbatch;
    @Bind(R.id.btproduce)
    Button btproduce;
    List<ProductInfo> pList = new ArrayList<ProductInfo>();
    ProductAdapter productAdapter = null;
    ProductInfo productInfo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relation_info);
        ButterKnife.bind(this);
        tvTitle.setText("生产对应");
        etbatch.setText(DateUtils.stampToDate(new Date().getTime()));

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    return;
                }
                for (int i = 0; i < pList.size(); i++) {
                    pList.get(i).setFlag(false);
                }
                pList.get(position - 1).setFlag(true);
                productInfo = pList.get(position - 1);
                notifyPlist(pList);
            }
        });

        etp.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.toString().length()!=0)
                {
                    presenter.showpidlist((charSequence.toString()));
                }
                else
                {
                    presenter.showplist();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        View headView = getLayoutInflater().inflate(R.layout.list_phead, null);
        listview.addHeaderView(headView);
        presenter.showplist();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public RelationInfoModel createModel() {
        return new RelationInfoModelImpl(this);
    }

    @Override
    public RelationInfoView createView() {
        return this;
    }

    @Override
    public RelationInfoPresent createPresenter() {
        return new RelationInfoPresent();
    }

    @OnClick({R.id.iv_title_left, R.id.btbatch, R.id.btproduce})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.btbatch:
                break;
            case R.id.btproduce:
                if(productInfo==null)
                {
                    showMsg("请选择产品");
                    return;
                }
                if(etbatch.getText().toString().length()<8)
                {
                    showMsg("批次必须至少8位");
                    return;
                }
                AbScanfBean abScanfBean = new AbScanfBean();
                abScanfBean.setBatch(etbatch.getText().toString());
                abScanfBean.setPid(productInfo.getIid());
                abScanfBean.setPname(productInfo.getName());
                abScanfBean.setCaseBoxNums(productInfo.getCaseBoxNums());
                abScanfBean.setBoxNum(0);
                abScanfBean.setNum(presenter.getnum(productInfo.getIid(),etbatch.getText().toString()));
                abScanfBean.setUpnum(presenter.getUpnum(productInfo.getIid(),etbatch.getText().toString()));
                intentToActivityWithSParameter(this,RelationScanfActivity.class, Util.scanfinfo,abScanfBean);
                break;
        }
    }

    @Override
    public void showPlist(List<ProductInfo> list) {
        pList = list;
        for (int i = 0; i < pList.size(); i++) {
            pList.get(i).setFlag(false);
        }
        productAdapter = new ProductAdapter(this,pList);
        listview.setAdapter(productAdapter);
    }

    @Override
    public void notifyPlist(List<ProductInfo> list) {
        pList = list;
        productAdapter.notifyDataSetChanged();
    }
}
